﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Hutech.Authentication.Library.Data;


namespace Hutech.Authentication.Library.Business
{
    public class RolePermission
    {

        #region Constructors

        public RolePermission()
        { }

        public RolePermission(
        Guid roleId,
        Guid actionGuid)
        {
            GetRolePermission(
                roleId,
                actionGuid);
        }

        public RolePermission(
            Guid roleId,
            Guid actionGuid,
            Guid rolePermissionGuid)
        {
            GetRolePermission(
                roleId,
                actionGuid,
                rolePermissionGuid);
        }

        #endregion

        #region Private Properties

        private Guid roleId = Guid.Empty;
        private Guid actionGuid = Guid.Empty;
        private Guid rolePermissionGuid = Guid.Empty;
        private DateTime editDate;
        private Guid editUserID = Guid.Empty;

        #endregion

        #region Public Properties

        public Guid RoleId
        {
            get { return roleId; }
            set { roleId = value; }
        }
        public Guid ActionGuid
        {
            get { return actionGuid; }
            set { actionGuid = value; }
        }
        public Guid RolePermissionGuid
        {
            get { return rolePermissionGuid; }
            set { rolePermissionGuid = value; }
        }
        public DateTime EditDate
        {
            get { return editDate; }
            set { editDate = value; }
        }
        public Guid EditUserID
        {
            get { return editUserID; }
            set { editUserID = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of RolePermission.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        private void GetRolePermission(
            Guid roleId,
            Guid actionGuid,
            Guid rolePermissionGuid)
        {
            using (IDataReader reader = DBRolePermission.GetOne(
                roleId,
                actionGuid,
                rolePermissionGuid))
            {
                PopulateFromReader(reader);
            }

        }

        /// <summary>
        /// Gets an instance of RolePermission.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        private void GetRolePermission(Guid roleId,Guid actionGuid)
        {
            using (IDataReader reader = DBRolePermission.GetOneBy2Para(roleId,actionGuid))
            {
                PopulateFromReader(reader);
            }

        }

        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                this.roleId = new Guid(reader["RoleId"].ToString());
                this.actionGuid = new Guid(reader["ActionGuid"].ToString());
                this.rolePermissionGuid = new Guid(reader["RolePermissionGuid"].ToString());
                this.editDate = Convert.ToDateTime(reader["EditDate"]);
                this.editUserID = new Guid(reader["EditUserID"].ToString());

            }

        }

        /// <summary>
        /// Persists a new instance of RolePermission. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            //this.roleId = Guid.NewGuid();
            //this.actionGuid = Guid.NewGuid();
            this.rolePermissionGuid = Guid.NewGuid();

            int rowsAffected = DBRolePermission.Create(
                this.roleId,
                this.actionGuid,
                this.rolePermissionGuid,
                this.editDate,
                this.editUserID);

            return (rowsAffected > 0);
        }


        /// <summary>
        /// Updates this instance of RolePermission. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBRolePermission.Update(
                this.roleId,
                this.actionGuid,
                this.rolePermissionGuid,
                this.editDate,
                this.editUserID);
        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of RolePermission. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            //if (this.roleId != Guid.Empty)
            //{
            //    return Update();
            //}
            //else
            //{
            //    return Create();
            //}
            //if (this.actionGuid != Guid.Empty)
            //{
            //    return Update();
            //}
            //else
            //{
            //    return Create();
            //}
            if (this.rolePermissionGuid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of RolePermission. Returns true on success.
        /// </summary>
        /// <param name="roleId"> roleId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="rolePermissionGuid"> rolePermissionGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(Guid rolePermissionGuid)
        {
            return DBRolePermission.Delete(
                rolePermissionGuid);
        }


        /// <summary>
        /// Gets a count of RolePermission. 
        /// </summary>
        public static int GetCount()
        {
            return DBRolePermission.GetCount();
        }

        private static List<RolePermission> LoadListFromReader(IDataReader reader)
        {
            List<RolePermission> rolePermissionList = new List<RolePermission>();
            try
            {
                while (reader.Read())
                {
                    RolePermission rolePermission = new RolePermission();
                    rolePermission.roleId = new Guid(reader["RoleId"].ToString());
                    rolePermission.actionGuid = new Guid(reader["ActionGuid"].ToString());
                    rolePermission.rolePermissionGuid = new Guid(reader["RolePermissionGuid"].ToString());
                    rolePermission.editDate = Convert.ToDateTime(reader["EditDate"]);
                    rolePermission.editUserID = new Guid(reader["EditUserID"].ToString());
                    rolePermissionList.Add(rolePermission);

                }
            }
            finally
            {
                reader.Close();
            }

            return rolePermissionList;

        }

        /// <summary>
        /// Gets an IList with all instances of RolePermission.
        /// </summary>
        public static List<RolePermission> GetAll()
        {
            IDataReader reader = DBRolePermission.GetAll();
            return LoadListFromReader(reader);
        }

        /// <summary>
        /// Gets an IList with page of instances of RolePermission.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<RolePermission> GetPage(int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBRolePermission.GetPage(pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

        #region Static methods add by tsandtm
        public static List<RolePermission> GetAllByRoleID(Guid RoleID)
        {
            IDataReader reader = DBRolePermission.GetAllByRoleID(RoleID);
            return LoadListFromReader(reader);
        }

        /// <summary>
        /// Check exist permission
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="ActionCommand"></param>
        /// <returns></returns>
        internal static bool CheckPermission(Guid RoleID, string ActionCommand)
        {
            return DBRolePermission.CheckPermission(RoleID, ActionCommand);
        }
        #endregion

        
    }

}
